#include "audiothread.h"
#include <QDebug>
#include <QAudioSink>
#include <QAudioFormat>
#include <QAudioInput>


namespace wxb{
AudioThread::AudioThread(QObject *parent)
    : QThread{parent}
{
}

AudioThread::~AudioThread()
{
    delete resample;
}

void AudioThread::addPktToCache(AVPacket *pkt)
{
    pktList.push_back(pkt);
}

bool AudioThread::openDecoder(AVCodecParameters* params)
{
    if(params == nullptr){
        qDebug() << "AudioThread::openDecoder(AVCodecParameters* params) params == nullptr";
        return false;
    }
    channelCount = params->ch_layout.nb_channels;
    sampleRate = params->sample_rate;
    //需要手动释放params
    this->params = params;
    return decode.openDecoder(params);
}

bool AudioThread::initQtPlay()
{
    QAudioFormat fmt;
    fmt.setSampleRate(sampleRate);
    fmt.setChannelCount(channelCount);
    fmt.setSampleFormat(QAudioFormat::Int16);
    sink = new QAudioSink(fmt);
    ioDevice = sink->start();
    if(ioDevice == nullptr){
        qDebug() << "AudioThread::AudioThread(QObject *parent) ioDevice == nullptr";
        return false;
    }
    return true;
}

bool AudioThread::initResample()
{
    if(resample == nullptr){
        if(params == nullptr){
            qDebug() << "AudioThread::run() params == nullptr";
            return false;
        }
        int ret = 0;
        resample = new Resample();
        ret = resample->initSwrCtx(params);
        if(!ret){
            qDebug() << "AudioThread::initResample() resample.initSwrCtx(params); error";
            return false;
        }
        avcodec_parameters_free(&params);
    }
    return true;
}

void AudioThread::run()
{
     qDebug() << "void AudioThread::run()";

     //=========================重采样 start ==========================
    bool ret = initResample();
     if(!ret){
        return;
    }
    //=========================qt播放 start ==========================
    ret = initQtPlay();
    if(!ret){
        return;
    }

    while (!exitFlag)
    {
        //没有数据
        if (pktList.empty()){
            continue;
        }
        //拿出第一个节点数据
        AVPacket *pkt = pktList.front();
        pktList.pop_front();
        bool ret = decode.sendToDecoder(pkt);
        if (!ret)
        {
            continue;
        }
        while (true)
        {
            AVFrame * frame = decode.receiveFrame();
            if (frame == nullptr){
                break;
            }
            //播放音频
            //采样大小 * 2通道 * 2字节深度
            int size = frame->nb_samples * frame->ch_layout.nb_channels * 2;
            unsigned char *data = new unsigned char[size];
            resample->transDataFmtRetSize(frame,data);
            av_frame_free(&frame);
            ioDevice->write((char*)data,size);
            //Stream #0:1: Audio: aac (LC), 8000 Hz, mono, fltp
            // qDebug() << "frame->ch_layout.nb_channels;" << frame->ch_layout.nb_channels;//frame->ch_layout.nb_channels; 1
            // qDebug() << "audio frame->linesize[0]" << frame->linesize[0];//8192
            // qDebug() << "audio frame->linesize[1]" << frame->linesize[1];//0
            // qDebug() << "audio frame->linesize[2]" << frame->linesize[2];//0
            delete[] data;
        }
    }
}
}
